#include "dmp.h"

dmp::dmp(QDate date_, track track_, QList<team*> t)
{
    standings_m.rider_points.resize(16);
    standings_m.rider_rn.resize(16);
    standings_m.rider_rt.resize(16);
    standings_m.rider_zz.resize(16);
    standings_m.team_points.resize(16);,
    standings_m.team_points[15].resize(2);
    table_of_heats.heats.resize(15);
    for (int i = 0; i < 16; i++)
    {
        standings_m.rider_rn[i] = 0;
        standings_m.rider_rt[i] = 0;
        standings_m.rider_zz[i] = 0;
    }

    for (int i = 0; i < 15; i++)
    {
        table_of_heats.heats[i].start_positions.resize(4);
        table_of_heats.heats[i].number = i;
        standings_m.team_points[i].resize(2);
        standings_m.team_points[i][0] = 0;
        standings_m.team_points[i][1] = 0;
    }
    QList<QStringList> table_from_file = CSV.read("rules/dmp.csv");
    for (int i = 0; i < table_from_file.size(); i++)
    {
        for (int n = 0; n < 4; n++)
        {
            table_of_heats.heats[i].number = i;
            table_of_heats.heats[i].start_positions[n].helmet = table_from_file[i][n][2];
                table_of_heats.heats[i].start_positions[n].rider_number = table_from_file[i][n].left(2).toInt()-1;
            table_of_heats.heats[i].start_positions[n].number = n;
        }
    }
    teams = t;
    date = date_;
    h = new heat(track_.name);
    event_type = "dmp";
    heat_number = 0;
    containers.resize(2);
}
bool dmp::isChangeValid(int riders_number, int position)
{
    bool valid = false;
    int r_num = table_of_heats.heats[heat_number].start_positions[position].rider_number;
    valid = ( ( ( (riders_number == 5) || (riders_number == 6) || (riders_number == 13) || (riders_number == 14) )
               && ( ( (!( (r_num == 5)||(r_num == 6) || (r_num == 13) || (r_num == 14) )) && ( ( (standings_m.rider_rn[riders_number] < 2) && (heat_number >= 5) )
               || ( (heat_number >= 5) && (standings_m.rider_rt[riders_number] < 1) ) ) ) ) )
        || ( (!( (riders_number == 5) || (riders_number == 6) || (riders_number == 13) || (riders_number == 14) ) )
             && (heat_number >= 5) && (!( (r_num == 5)||(r_num == 6) || (r_num == 13) || (r_num == 14) ) ) ) ) ;
    //!!!liczenie zmian zwyklych, taktycznych itd. TO DO
    return valid;
}
QStringList dmp::getRidersTextRepresentation()
{
    QStringList riders;
    for (int i = 0; i < 7; i++)
    {
        riders.append(riders_list[i]->surname);
    }
    return riders;
}
void dmp::doSomethingAfterHeat()
{
    for (int i = 0; i < 4; i++)
    {
        if (table_of_heats.heats[heat_number].start_positions[i].rider_number < 8)
            standings_m.team_points[heat_number][0] += standings_m.rider_points[table_of_heats.heats[heat_number].start_positions[i].rider_number].last();
        else
            standings_m.team_points[heat_number][1] += standings_m.rider_points[table_of_heats.heats[heat_number].start_positions[i].rider_number].last();
    }
    standings_m.team_points[15][0] += standings_m.team_points[heat_number][0];
    standings_m.team_points[15][1] += standings_m.team_points[heat_number][1];
}
